package dao.impl;

import dao.CommunityDao;
import entity.Community;
import utils.DBconn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CommunityDaoImpl implements CommunityDao{
    @Override
    public boolean insert(Community community) {
        if (community == null) return false;

        try
        {
            // 初始化连接
            DBconn.init();
            String tmp = " Insert Into dailing(projectname,customer,staff,servicenum,info,starttime,finishtime,explainInfo) VALUE(" +
                    "'"+community.getProjectname()+"',"+
                    "'"+community.getCustomer()+"',"+
                    "'"+community.getStaff()+"',"+
                    "'"+community.getServicenum()+"',"+
                    "'"+community.getInfo()+"',"+
                    "'"+community.getStarttime()+"',"+
                    "'"+community.getFinishtime()+"',"+
                    "'"+community.getExplain()+"' )";
            DBconn.addUpdDel(tmp);
            DBconn.closeConn();
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean delete(int id) {
        try
        {
            // 初始化连接
            DBconn.init();
            DBconn.addUpdDel("Delete form dailing Where id="+id);
            DBconn.closeConn();
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean delete(Community community) {
        if (community == null) return false;
        return delete(community.getId());
    }

    @Override
    public boolean update(Community community) {

        if (community == null) return false;

        else try
        {
            // 初始化连接
            DBconn.init();
            DBconn.addUpdDel(" Update dailing Set " +
                    "id='"+community.getId()+"',"+
                            "projectname='"+community.getProjectname()+"',"+
                            "customer='"+community.getCustomer()+"',"+
                            "staff='"+community.getStaff()+"',"+
                            "servicenum='"+community.getServicenum()+"',"+
                            "info='"+community.getInfo()+"',"+
                            "starttime='"+community.getStarttime()+"',"+
                            "finishtime='"+community.getFinishtime()+"',"+
                            "explainInfo='"+community.getExplain()+"'"+
                            "' where id='" + community.getId()+"'"
//                    "date=to_date('"+sdf.format(Community.getDate())+"', 'yyyy-mm-dd hh24:mi:ss')"+
//                    " Where id=" + community.getId()
            );

            DBconn.closeConn();
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public List<Community> getAllcommunity(int page,int limit) {
        List<Community> list = new ArrayList<Community>();

        try {
            DBconn.init();
            ResultSet resultSet = DBconn.selectSql("select * from dailing limit "+(page-1)*limit+","+limit+" ");
            while (resultSet.next()) {
                Community community = new Community();
                community.setId(resultSet.getInt("id"));
                community.setProjectname(resultSet.getString("projectname"));
                community.setCustomer(resultSet.getString("customer"));
                community.setStaff(resultSet.getString("staff"));
                community.setServicenum(resultSet.getString("servicenum"));
                community.setInfo(resultSet.getString("info"));
                community.setStarttime(resultSet.getString("starttime"));
                community.setFinishtime(resultSet.getString("finishtime"));
                community.setExplain(resultSet.getString("explainInfo"));
                list.add(community);
            }
            DBconn.closeConn();
            return list;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
    public int getCountByCommunity(){
        int count = 0;
        try {
            DBconn.init();
            ResultSet rs = DBconn.selectSql("select count(*) from dailing ");
            if(rs.next()){
                count = rs.getInt(1);

            }
            DBconn.closeConn();
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public boolean select(Community community) {
        boolean flag=false;
        try {
            DBconn.init();
            ResultSet resultSet = DBconn.selectSql("select * from dailing where projectname='"+community.getProjectname()+"'");
            while(resultSet.next()){
                if(resultSet.getString("projectname").equals(community.getProjectname())){
                    flag = true;
                }
            }
            DBconn.closeConn();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return flag;
    }
    public boolean deleteById(List list) {
        boolean flag = false;
        DBconn.init();
        String sql = "delete  from dailing where id=" + list.get(0);

        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    public boolean deleteByIds(List list) {

        boolean flag = false;
        DBconn.init();
        StringBuilder sql = new StringBuilder("delete  from dailing where id in( ");
        for (int i = 0; i < list.size(); i++) {
            sql.append("" + list.get(i) + ",");
        }

        sql.replace(sql.lastIndexOf(","), sql.lastIndexOf(",") + 1, ")");

        int i = DBconn.addUpdDel(sql.toString());
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }



}

